package com.lovo.ssc.service.impl;

import com.alibaba.fastjson.JSON;
import com.lovo.ssc.bean.OrderGoods;
import com.lovo.ssc.service.IOrderGoodsService;
import com.lovo.ssc.service.IOrderService;
import com.lovo.ssc.vo.CartItem;
import com.lovo.sscweb.dto.OrderDto;
import com.lovo.sscweb.dto.OrderItemTo;
import com.lovo.util.MQUtil;
import com.rabbitmq.client.Channel;
import ma.glasnost.orika.MapperFacade;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * @author WXL
 * @date 2022/7/13
 */
@Service
public class OrderResultService {
    @Autowired
    private CartService cartService;

    @Autowired
    private MapperFacade mapperFacade;

    @Autowired
    private IOrderGoodsService orderGoodsService;

    @Autowired
    private IOrderService orderService;

    @RabbitListener(queues = MQUtil.ExpenditureQueue)
    public String orderResult(Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag, String orderDto) throws IOException {
        System.out.println(orderDto);
        OrderDto dto = JSON.parseObject(orderDto, OrderDto.class);
        if (dto != null && dto.getOrderStatus() != 0) {
            //修改订单状态
            orderService.updateOrderState(dto.getOrderCode());
            return "支付成功";
        }
        channel.basicAck(tag, false);
        return "支付失败,余额不足";
    }
}
